package com.qf.petmall.starter.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.petmall.starter.entity.ShopCart;
import com.qf.petmall.starter.homeclass.entity.Homeclass;
import com.qf.petmall.starter.homeclass.service.IHomeclassService;
import com.qf.petmall.starter.service.IShopCartService;
import com.qf.petmall.starter.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-12-08
 */
@RestController
@Api(tags="购物车模块")
@RequestMapping("/shop_cart")
public class ShopCartController {
	@Autowired
	IShopCartService iShopCartService;

	@Autowired
	IHomeclassService iHomeclassService;


	@Autowired
	StringRedisTemplate redisTemplate2;

	@ApiOperation(value="添加购物车")
	@PostMapping("/add")
	public R addCart( @RequestBody ShopCart shopCart ) {

		Integer productId=shopCart.getProductId( );

		//判断是否是同一个用户和同一件商品
		ShopCart shopCart1=iShopCartService.getOne( new QueryWrapper< ShopCart >( ).eq( "product_id",productId ).eq( "user_id",shopCart.getUserId( ) ) );
		if(shopCart1!=null) {

			//进行价格和数量的增加
			int i=shopCart1.getProductNum( )+shopCart.getProductNum( );
			shopCart.setProductNum( i );
			shopCart.setProductProp( "默认" );
			shopCart.setId( shopCart1.getId( ) );

			//生成一个下单时间
			shopCart.setCartTime( new Date( ) );
			boolean b=iShopCartService.updateById( shopCart );
			if(b) {
				return R.ok( ).put( "shopCartId",shopCart.getId( ) );
			}
		}else {
			shopCart.setProductProp( "默认" );

			//生成一个下单时间
			shopCart.setCartTime( new Date( ) );
			boolean save=iShopCartService.save( shopCart );
			if(save) {
				return R.ok( ).put( "shopCartId",shopCart.getId( ) );
			}
		}


		return R.error( "操作有误！！" );
	}


	@ApiOperation(value="根据购物车的id删除购物车")
	@PostMapping("/del")
	public R delCart( @RequestBody List< Integer > cartIds ) {
		R r=iShopCartService.deleteCartsByid( cartIds );
		return r;

	}

	@ApiOperation(value="查看购物车，根据用户id")
	@GetMapping("/list")
	public R listALl( String userId ) {
		if (userId == null) {
		    return R.error("请传入用户ID");
		}
		int i=Integer.parseInt( userId );

		//先去redis里面查询如果有直接反
		String o=redisTemplate2.opsForValue( ).get( i+"" );

		if(StrUtil.isNotBlank( o )) {
			//存在直接返回
			List<ShopCart> shop=JSON.parseObject( o,List.class );
			return R.ok( ).put( "cartList",shop );
		}

		//没有查到就进入数据库里面进行查找
		QueryWrapper< ShopCart > QueryWrapper=new QueryWrapper<>( );
		QueryWrapper.eq( "user_id",i );
		List< ShopCart > list=iShopCartService.list( QueryWrapper );
		List< Integer > productIds=list.stream( ).map( s->s.getProductId( ) ).collect( Collectors.toList( ) );
		List< Homeclass > Products=iHomeclassService.list( new QueryWrapper< Homeclass >( ).in( "id",productIds ) );
		Map< Integer, Homeclass > homeMap=Products.stream( ).collect( Collectors.toMap( s->s.getId( ),s->s ) );
		for(ShopCart shopCart: list) {
			shopCart.setProductName( homeMap.get( shopCart.getProductId() ).getName() );
			shopCart.setHomeClass( homeMap.get( shopCart.getProductId() ).getShopClass() );
		}


		String s=JSON.toJSONString( list );
		//查到了之后，先存入redis
		redisTemplate2.opsForValue().set( i+"",s,30L,TimeUnit.MINUTES );

		//然后进行返回
		return R.ok( ).put( "cartList",list );
	}



}
